//@note 地址选择组件
//@author czh
put('location',function(){

	var headSelect = '<div class="w-noc-head-tab"><span class="w-noc-head-item w-noc-head-item-active">选择市</span><span class="w-noc-head-item">选择省</span></div></div>' ;
	var headStr = '<div class="w-noc-head"><a id="wLocFinal" class="w-noc-arrow iconfont" href="javascript:void(0);">&#xf0292;</a>' + headSelect ;
	var currentLocation = '<div class="w-loc-body"><div class="w-loc-title">当前位置</div><div id="nowLocation" class="w-loc-list"></div></div>' ;
	
	var provinceStr = '<div id="widgetList" class="w-loc-list"></div>' ;
	var bodyStr = '<div class="w-loc-body"><div class="w-loc-title">按省份选择城市</div>' + provinceStr + '</div>' ;
	var selectHead = '<div class="w-loc-selectHead">已选地区</div>' ;
	var selectBody = '<div id="wLocSelect" class="w-loc-selectBody"></div>' ;
	var selectStr = '<div class="w-loc-select w-loc-selectGod">' + selectHead + selectBody + '</div>' ;
	var htmlStr = '<div id="widgetLoc" class="w-loc">'  + 
					headStr + selectStr + currentLocation + bodyStr + '</div>' ;
	
	$('body').append(htmlStr) ;
	
	var cityHead = '<div class="w-loc-head"><a id="wCityHeadBack" class="w-loc-arrow iconfont" href="javascript:void(0);">&#xf0292;</a><span class="w-loc-txt">选择地区</span></div>';
	var cityBody = '<div class="w-loc-body"><div class="w-loc-title"></div></div>' ;
	var cityList = '<div id="widgetCityList" class="w-loc-list"><div>' ;
	var citysStr = '<div style="z-index:30;left:21rem;" id="widgetCitys" class="w-loc">' + cityHead + cityBody + cityList + '</div>';
	
	$('body').append(citysStr);

	var conf = {
		single : false ,
		city : true
	} ;

	$('.w-noc-head-item').each(function(idx){
		$(this).on('click',function(){
			if(idx===0){
				conf.city = true ;
			} else {
				conf.city = false ;
			}
			var cls = 'w-noc-head-item-active' ;
			$('.w-noc-head-item').removeClass(cls) ;
			$(this).addClass(cls) ;
			$('#wLocSelect').empty() ;
		}) ;
	}) ;

	var emitCityEvent = function(){
		$('.m-city-item').on('click',function(){				
			var selectClass = 'w-loc-citySel' ;

			//@note 地址单选
			if( conf.single ){
				var dataEle = $(this) ;
				finalCall([{id:dataEle.attr('data-id'),'name':dataEle.html()}]) ;
				$('#widgetLoc').css('left','21rem');
				bodyCss.reset();
				$('#widgetCitys').css('left','21rem') ;
				return ;
			}

			if($(this).hasClass(selectClass)){
				$(this).removeClass(selectClass) ;
			}else{
				$(this).addClass(selectClass) ;
			}

		});		
	} ;
	$('#wCityHeadBack').on('click',function(){
		$('#widgetCitys').css('left','21rem') ;
	}) ;
	var emitProEvent = function(){
		$('.m-pro-item').on('click',function(){
			var cityLabel = $('#widgetCitys') ;
			var node = $(this).find('.w-loc-txtWrap') ;
			if( !conf.city ){
				if( !conf.single ){
					var p = $('#wLocSelect') ;
					var some = $('#wLocSelect').find('.w-loc-selPoint') ;
					for( var i = 0 ; i < some.length ; i++ ){
						if( node.attr('data-id') === some.eq(i).attr('data-id') ){
							return  ;
						}
					}
					p.append('<span data-id="' + node.attr('data-id') + '" class="w-loc-selPoint">' + node.html() + '</span>') ;
				}else{
					finalCall([{id:node.attr('data-id'),name:node.html()}]) ;
					$('#widgetLoc').css('left','21rem');
					bodyCss.reset();
				}
				return  ;
			}
			cityLabel.css('left','0') ;
			cityLabel.find('.w-loc-title').html(node.html()) ;
			$.ajax({
				url : window.appRoot + '/location/getCity.html' ,
				method : 'get' ,
				data : {
					pid : node.attr('data-id')
				} ,
				success : function(list){
					var panel = $('#widgetCityList') ;
					panel.html('') ;
					var createItem = function(json){
						return '<a href="javascript:void(0);" class="m-city-item w-loc-item"><span data-id="'+json.id+'" class="w-loc-txtWrap">'+json.name+'</span><span class="w-locl-gou" >&radic;</span></a>';
						} ;
					for(var i = 0 ; i < list.length ; i++ ){
						panel.append(createItem(list[i])) ;
					}
					emitCityEvent() ;
				}
			}) ;
		}) ;
	} ;
	$.ajax({
		url : window.appRoot + '/location/province.html' ,
		method : 'get' ,
		dataType : 'json' ,
		success : function( list ){
			var createItem = function(json){
				return '<a href="javascript:void(0);" class="m-pro-item w-loc-item"><span data-id="'+json.id+'" class="w-loc-txtWrap">'+json.name+'</span><span class="w-locl-arrow iconfont">&#xf016d;</span></a>';
			} ;
			for(var i = 0 ; i < list.length ; i++ ){
				$('#widgetList').append(createItem(list[i])) ;
			} 
			emitProEvent() ;
		}
	}) ;
	
	$('#nowLocation').html('<div class="w-loc-current"><span class="w-loc-txtWrap">全国</span></div>') ;
	
	var bodyCss = {
		_ele : $('body') ,
		set : function(){
			this._ele.css('overflow','hidden');
			var self = this ;
			window.setTimeout(function(){
				$('.w-loc').css({
					'height' : $(window).height()
				});
			},1000) ;
		} ,
		reset : function(){
			this._ele.css('overflow','auto');
		}
	} ;
	
	$('#wCityHeadBack').on('click',function(){
		var items = $('#widgetCityList .w-loc-item');
		var p = $('#wLocSelect') ; 
		var data = (function(){
			var obj = {} ;
			var some = p.find('.w-loc-selPoint') ;
			for(var i = 0 ; i < some.length ;i++ ){
				obj[some.eq(i).attr('data-id')] = true ;
			}
			return obj ;
		}()) ;
		for(var i = 0 ; i < items.length ; i++ ){
			if(items.eq(i).hasClass('w-loc-citySel') ){
				var point = items.eq(i).find('.w-loc-txtWrap') ;
				if( !( point.attr('data-id') in data ) ){
					p.append('<span data-id="' + point.attr('data-id') + '" class="w-loc-selPoint">' + point.html() + '</span>') ;
				}
			} 
		}
	});
	
	$('#wLocSelect').on('click','.w-loc-selPoint',function(){
		$(this).remove();
	})
	var finalCall = function(){} ;
	$('#wLocFinal').on('click',function(){
		var list = [] ;
		$('#wLocSelect .w-loc-selPoint').each(function(){
			var me = $(this) ;
			list.push({id:me.attr('data-id'),name:me.html()}) ;
		}) ;
		finalCall(list);
		$('#widgetLoc').css('left','21rem');
		bodyCss.reset();
	}) ;

	return {
		open : function(callback,option){

			//@note 设置参数及初始化
			if(option){
				conf.single = option.single===true;
				conf.city = option.city === false ? false : true ;
			}
			if( conf.single ){
				$('.w-loc-selectGod').remove();
			}

			var wrap = $('#widgetLoc') ;
			try{
				wrap[0].clientLeft ;
			}catch(ev){}
			if( typeof callback === 'function' ){
				finalCall = callback ;
			}
			wrap.css({'left':'0'}) ;
			bodyCss.set() ;
		},
		close : function(){
			var wrap = $('#widgetLoc') ;
			wrap.css('left','21rem') ;
			bodyCss.reset() ;
		}
	} ;

}) ;